Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the numba cache to be used, for development #441

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

hyanwong
Copy link
Member

@hyanwong hyanwong commented Dec 1, 2024

Copied from SGKIT. Fixes #438

@hyanwong hyanwong mentioned this pull request Dec 1, 2024
@nspope
Copy link
Contributor

nspope commented Dec 1, 2024

I ran into the same issues as you here-- the bulk of stuff is still being compiled on import. I think pulling all the EP stuff into a low-level extension is the way to go.

@hyanwong
Copy link
Member Author

hyanwong commented Dec 1, 2024

I think pulling all the EP stuff into a low-level extension is the way to go.

Can we do this while keeping it in python (I presume not)? Or are you thinking of writing the low-level e.g. in rust?

@nspope
Copy link
Contributor

nspope commented Dec 1, 2024

It may be possible to build the extension with numba, but I think it'd be stabler and easier to maintain a C or rust extension.

@nspope
Copy link
Contributor

nspope commented Jan 16, 2025

I could make the edits to the scipy special functions on this branch if you'd like @hyanwong ?

@hyanwong
Copy link
Member Author

Yes, why not. Thank you.

@hyanwong
Copy link
Member Author

Do you want me to have a go at swapping in math.lgamma for the script version, @nspope ?

@nspope
Copy link
Contributor

nspope commented Jan 20, 2025

Sure, sorry to drop the ball on this. Where I'm thinking is, https://github.com/tskit-dev/tsdate/blob/240593d01a1a77561dadebee82cfb5fae67f83bc/tsdate/hypergeo.py#L72C17-L74C26

the _gammaln_f8 should be changed to math.lgamma -- hopefully that's enough to make more functions cache-able?

@hyanwong
Copy link
Member Author

No problem,. I'll give it a whirl. Thanks for the pointer.

@hyanwong
Copy link
Member Author

hyanwong commented Jan 20, 2025

OK, with the new commits and the math.lgamma function this now seems to work, and cuts import down from 29 seconds (with TSDATE_DISABLE_NUMBA_CACHE=1) to 9 seconds (with TSDATE_DISABLE_NUMBA_CACHE=0) on my laptop.

I put _hyp2f1_unity as an inner function within _hyp2f1_laplace, as I couldn't figure out how to pass things neatly otherwise. I guess you could rework that if it grates or is expected to make things slow @nspope. I haven't figured out how to make _gammainc_inv (and the related approximate_gamma_iqr) cacheable either, so that would make loading faster too, if you can figure it out.

@hyanwong
Copy link
Member Author

Aha. I spotted some more functions I could mark to cache. Now down to 5 secs for import.

@hyanwong
Copy link
Member Author

I guess this is good to go, at least for the time being.

To enable the caching (e.g. for development), you need to explicitly set TSDATE_DISABLE_NUMBA_CACHE=0 which seems a bit of a weird way to phrase it (I would have chosen TSDATE_ENABLE_NUMBA_CACHE=1 I think). I just copied that from sgkit: is there any reason for the choice of name / polarisation in sgkit, @jeromekelleher ?

@jeromekelleher
Copy link
Member

I don't think there's a good reason for the choice of name, I agree with your preference for TSDATE_ENABLE_NUMBA_CACHE=1

@hyanwong hyanwong force-pushed the numba-cache branch 2 times, most recently from a3ac756 to c58a15d Compare January 21, 2025 13:43
@hyanwong
Copy link
Member Author

@nspope - I'm happy to merge this. Loading time for the library is now down to 2.5s on my laptop with TSDATE_ENABLE_NUMBA_CACHE=1. Does it look OK to you?

Uses math.lgamma to allow numba caching. Copied from SGKIT. Fixes tskit-dev#438
Copy link
Contributor

@nspope nspope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks @hyanwong !

@hyanwong hyanwong merged commit 8b8d393 into tskit-dev:main Jan 21, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reduce import time
3 participants